package Controller;

import Model.*;
import Service.StaticDataImportService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 *
 * 跳转到统计数据导入 dbf文件上传处理解析存入mysql数据库
 */

@Controller
@RequestMapping("/toStaticDataImport")
public class StaticDataImportController {

    @Autowired
    StaticDataImportService staticDataImportService;


    //护理评估申请表dbf导入
    @RequestMapping("/estimateApply")
    @ResponseBody
    public MsgJson uploadestimateApply(MultipartFile file, HttpServletRequest request) throws IOException, ParseException {
        //解析dbf文件存入list
        List<EstimateApply> estimateApplies=staticDataImportService.getEstimateApplyList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertEstimateApply(estimateApplies);
        if(result==estimateApplies.size()){
            System.out.println("批量插入评估申请表成功");
        }
        return MsgJson.success();
    }

    //护理评估结果表dbf导入
    @RequestMapping("/estimateResult")
    @ResponseBody
    public MsgJson uploadestimateResult(MultipartFile file, HttpServletRequest request) throws IOException {
        //解析dbf文件存入list
        List<EstimateResult> estimateResults=staticDataImportService.getEstimateResultList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertEstimateResult(estimateResults);
        if(result==estimateResults.size()){
            System.out.println("批量插入评估结果表成功");
        }
        return MsgJson.success();
    }

    //护理计划表dbf导入
    @RequestMapping("/upload")
    @ResponseBody
    public MsgJson uploadDbf(MultipartFile file, HttpServletRequest request) throws Exception{
        //解析dbf文件存入list
        List<CarePlan> carePlans=staticDataImportService.getCarePlanList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertDbfData(carePlans);
        if(result==carePlans.size()){
            System.out.println("批量插入护理计划表成功");
        }
        return MsgJson.success();
    }

    //护理内容表dbf导入
    @RequestMapping("/careContent")
    @ResponseBody
    public MsgJson uploadcareContent(MultipartFile file, HttpServletRequest request) throws IOException {
        //解析dbf文件存入list
        List<CareContent> careContents=staticDataImportService.getCareContentList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertCareContent(careContents);
        if(result==careContents.size()){
            System.out.println("批量插入护理内容表成功");
        }
        return MsgJson.success();
    }

    //护理交易记录表dbf导入
    @RequestMapping("/careDeal")
    @ResponseBody
    public MsgJson uploadcareDeal(MultipartFile file, HttpServletRequest request) throws IOException {
        //解析dbf文件存入list
        List<CareDeal> careDeals=staticDataImportService.getCareDealList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertCareDeal(careDeals);
        if(result==careDeals.size()){
            System.out.println("批量插入护理交易记录表成功");
        }
        return MsgJson.success();
    }

    //老人去世信息dbf导入
    @RequestMapping("/elderDead")
    @ResponseBody
    public MsgJson uploadelderDead(MultipartFile file, HttpServletRequest request) throws IOException, ParseException {
        //解析dbf文件存入list
        List<ElderDead> elderDeads=staticDataImportService.getElderDeadList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertElderDead(elderDeads);
        if(result==elderDeads.size()){
            System.out.println("批量插入老人去世信息成功");
        }
        return MsgJson.success();
    }
    @RequestMapping("/elderHospital")
    @ResponseBody
    public MsgJson uploadelderHospital(MultipartFile file, HttpServletRequest request) throws IOException, ParseException {
        //解析dbf文件存入list
        List<ElderHospital> elderHospitals=staticDataImportService.getElderHospitalList(file);
        //解析成的list导入mysql
        int result=staticDataImportService.insertElderHospital(elderHospitals);
        if(result==elderHospitals.size()){
            System.out.println("批量插入老人住院信息成功");
        }
        return MsgJson.success();
    }


    //查询已入库的老人去世月份数据
    @RequestMapping("/deadmonth")
    @ResponseBody
    public PageInfo deadMonth(@RequestParam(value = "year",defaultValue = "")String year){
        List<MonthData> monthList=staticDataImportService.selectElderDeadMonth(year);
        PageInfo page=new PageInfo(monthList);
        return page;
    }

    //查询已入库的老人去世月份数据
    @RequestMapping("/hospitalmonth")
    @ResponseBody
    public PageInfo hospitalMonth(@RequestParam(value = "year",defaultValue = "")String year){
        List<MonthData> monthList=staticDataImportService.selectElderHospitalMonth(year);
        PageInfo page=new PageInfo(monthList);
        return page;
    }



    //查询已入库的月份数据
    @RequestMapping("/selectmonth")
    @ResponseBody
    public PageInfo selectMonth(@RequestParam(value = "tableName",defaultValue = "")String tableName,@RequestParam(value = "year",defaultValue = "")String year){

        List<MonthData> monthList=new ArrayList<MonthData>();
        PageInfo page=new PageInfo(monthList);
        //查询评估申请表已入库数据
        if (tableName.equals("1")){
            monthList=staticDataImportService.selectEstimateApplyMonth(year);
            page=new PageInfo(monthList);
        }
        //查询评估结果表已入库数据
        if (tableName.equals("2")){
            monthList=staticDataImportService.selectEstimateResultMonth(year);
            page=new PageInfo(monthList);
        }
        //查询护理计划表已入库数据
        if (tableName.equals("3")){
            monthList=staticDataImportService.selectMonthList(year);
            page=new PageInfo(monthList);
        }
        //查询护理内容表已入库数据
        if (tableName.equals("4")){
            monthList=staticDataImportService.selectCareContentMonth(year);
            page=new PageInfo(monthList);
        }
        //查询护理交易记录表已入库数据
        if (tableName.equals("5")){
            monthList=staticDataImportService.selectCareDealMonth(year);
            page=new PageInfo(monthList);
        }
        return page;
    }



}
